import os
import datetime
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from multiprocessing import Pool
from model import *
from config import config_scene
import warnings
warnings.filterwarnings("ignore")
config01 = config_scene()


def task_days():
    start_t = time.time()

    custflows_orig_all = pd.read_csv('custflows.csv')
    custflows_orig_all.hinfluence = custflows_orig_all.hinfluence.fillna(0)
    # custflows_orig_all.holiday_today_cn = custflows_orig_all.holiday_today_cn.astype("category")
    # custflows_orig_all = custflows_orig_all[custflows_orig_all.shop_id.map(lambda x: x not in ['9M8L', '9M8K'])]

    ############################################ 空值处理 ###############################################################
    # custflows_orig_all = custflows_orig_all.fillna(0)
    # for col in custflows_orig_all.columns:
    #     print(col, len(custflows_orig_all),sum(custflows_orig_all[col].isnull()))

    ############################################ 特征筛选 ###############################################################
    # custflows_orig_all = custflows_orig_all[custflows_orig_all.shop_id=='9M8A'] #  9M8K、 9M8I
    # hh = custflows_orig_all.corr()
    # hh = hh.sort_values('custflows')
    # custflows_orig_all = custflows_orig_all.drop('day_weather_level', axis=1) #  day_weather_level 0.071989
    # custflows_orig_all = custflows_orig_all.drop('night_weather_level', axis=1) #  night_weather_level  0.028034
    # custflows_orig_all = custflows_orig_all.drop('workday', axis=1) #  workday - 0.062177
    # custflows_orig_all = custflows_orig_all.drop('month', axis=1) #  month 0.060655
    # custflows_orig_all = custflows_orig_all.drop('duration', axis=1) #  duration 0.029402
    # custflows_orig_all = custflows_orig_all.drop('year', axis=1) #  year - 0.016407
    # custflows_orig_all = custflows_orig_all.drop('daily', axis=1) #  daily 0.008644

    custflows_result_lst = []
    for shop_id, custflows_orig in custflows_orig_all.groupby('shop_id'):

        start_day = config01.start_day
        # week_num = config01.week_num
        start_add = config01.start_add
        end_add = config01.end_add
        lines_up = config01.lines_up
        lines_down = config01.lines_down
        holiday_cols = config01.holiday_cols

        custflows_test_lst = []
        start_tuesday = datetime.datetime.strptime(str(start_day), '%Y%m%d')
        for i in range(1):
            start_tuesday_int = int(start_tuesday.strftime('%Y%m%d'))

            ############################################# 训练数据 ######################################################################
            label_name = 'custflows_tr'
            custflows_train = custflows_orig[custflows_orig.date <= start_tuesday_int]
            custflows_train = custflows_train[custflows_train.custflows <= 10 * custflows_train.custflows.mean()]
            custflows_train = custflows_train[custflows_train.custflows >= 0.1 * custflows_train.custflows.mean()]
            ###################################### 训练数据 划分 ########################################################################
            custflows_train01 = custflows_train[custflows_train.hinfluence.map(lambda x: abs(x)) <= lines_up]
            custflows_train01 = custflows_train01[custflows_train01[label_name].isnull() == False]
            custflows_train01 = custflows_train01[custflows_train01.scope_t > 0]
            custflows_train01 = custflows_train01.reset_index(drop=True)
            custflows_trainxy01 = custflows_train01.copy()
            custflows_trainxy01 = custflows_trainxy01.drop('shop_id', axis=1)
            custflows_trainxy01 = custflows_trainxy01.drop('date', axis=1)
            custflows_trainxy01 = custflows_trainxy01.drop('custflows', axis=1)

            ############################################# 测试数据 ######################################################################
            start_i_int = int((start_tuesday + datetime.timedelta(days=start_add)).strftime('%Y%m%d'))
            end_i_int = int((start_tuesday + datetime.timedelta(days=end_add)).strftime('%Y%m%d'))
            custflows_test = custflows_orig[(start_i_int <= custflows_orig.date) & (custflows_orig.date <= end_i_int)]
            custflows_test = custflows_test[custflows_test.custflows.isnull() == False]
            # custflows_test = custflows_test[custflows_test.pcustflows.isnull() == False]
            ###################################### 测试数据 划分 ########################################################################
            custflows_test01 = custflows_test[custflows_test.hinfluence.map(lambda x: abs(x)) <= lines_up]
            custflows_test01 = custflows_test01.reset_index(drop=True)
            custflows_testxy01 = custflows_test01.copy()
            custflows_testxy01 = custflows_testxy01.drop('shop_id', axis=1)
            custflows_testxy01 = custflows_testxy01.drop('date', axis=1)
            custflows_testxy01 = custflows_testxy01.drop('custflows', axis=1)

            ############################################# 模型划分训练 ######################################################################
            if len(custflows_testxy01) > 0:
                feats = [i for i in custflows_trainxy01.columns]
                feats.remove(label_name)
                if shop_id in ['9M8K']:
                    pred_y01, oof01 = xgb_foldtrain_regression(custflows_trainxy01, custflows_testxy01, feats, label_name)
                else:
                    pred_y01, oof01 = lgb_foldtrain_regression(custflows_trainxy01, custflows_testxy01, feats, label_name)
                custflows_test01['preds'] = pred_y01
                custflows_test01['sdt'] = start_tuesday_int
                custflows_test01['preds'] = custflows_test01['preds'] * custflows_test01.scope_t + custflows_test01.segment_min
                custflows_test_lst.append(custflows_test01[['shop_id', 'date', 'custflows', 'preds', 'sdt']])

            start_tuesday = start_tuesday + datetime.timedelta(days=7)

        custflows_test_df = pd.concat(custflows_test_lst, ignore_index=True)
        custflows_test_df = custflows_test_df.sort_values('date')
        custflows_test_df = custflows_test_df.reset_index(drop=True)
        custflows_result_lst.append(custflows_test_df)

        # plt.plot(custflows_test_df['pcustflows'], color="yellow")
        # plt.plot(custflows_test_df['custflows'], color="red")
        # plt.plot(custflows_test_df['preds'], color="blue")
        # plt.legend(['Old Predicted custflows', 'Actual custflows', 'Predicted custflows'])
        # plt.title(shop_id)
        # plt.savefig('result_test/{}.png'.format(shop_id))  # plt.show()
        # plt.close()



    ############################################# 保存测试结果 ######################################################################
    custflows_result = pd.concat(custflows_result_lst,ignore_index = True)
    custflows_result = custflows_result[['shop_id','date','preds','sdt']]
    custflows_result = custflows_result.rename(columns={'date': 'date_id', 'preds': 'custflows'})
    custflows_result = custflows_result.astype(str)
    custflows_result.date_id = custflows_result.date_id.map( lambda x: datetime.datetime.strptime(x, '%Y%m%d').strftime('%Y-%m-%d'))
    custflows_result.to_csv('custflows_result.csv',index=False)

    ############################################# 试结果指标计算 ######################################################################
    # score_lst = []
    # for custflows_test_df in custflows_result_lst:
    #     shop_id = custflows_test_df.shop_id.iloc[0]
    #     test_score = metrics.mean_absolute_percentage_error(custflows_test_df['custflows'], custflows_test_df['preds'])
    #     test_score_p = metrics.mean_absolute_percentage_error(custflows_test_df['custflows'], custflows_test_df['pcustflows'])
    #     score_lst.append([shop_id, test_score, test_score_p])
    #     print(test_score, test_score_p)
    #
    # scored_df = pd.DataFrame(score_lst,columns=['shop_id','score_test','score_old',]) # score_val
    # print('新模型mape',scored_df.score_test.mean(),'原模型mape',scored_df.score_old.mean())
    # scored_df['err'] = scored_df.score_test - scored_df.score_old
    # scored_df = scored_df.sort_values('err')
    # scored_df01 = scored_df[scored_df.err <= 0]
    # scored_df02 = scored_df[scored_df.err > 0]
    # print("共{}家门店效果更好，平均mape优于原模型{}个百分点".format(len(scored_df01),round(abs(scored_df01.err.mean())*100,2)))
    # print("共{}家门店效果更差，平均mape差于原模型{}个百分点".format(len(scored_df02),round(scored_df02.err.mean()*100,2)))
    # scored_df.to_csv('scored_df.csv',index=False)



    print('model_days time:', time.time()-start_t)



if __name__ == '__main__':
    task_days()





